//
//  LCAlertPickerView.m
//  LCInputView
//
//  Created by liangrongchang on 2017/4/19.
//  Copyright © 2017年 Rochang. All rights reserved.
//

#import "LCAlertPickerView.h"
#import "UIView+LCLayout.h"

@interface LCAlertPickerView ()<UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *maskView;
@property (weak, nonatomic) IBOutlet UIView *tipView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipViewHeight;
@property (nonatomic, strong) NSArray <NSString *>*dataSource;
@property (nonatomic, copy) selectedBlock selected;
@property (nonatomic, copy) NSString *selectedStr;

@end

@implementation LCAlertPickerView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.cancelButton setTitle:Language(@"取消") forState:UIControlStateNormal];
    [self.confirmButton setTitle:Language(@"确定") forState:UIControlStateNormal];
    
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(maskViewDidClick)];
    [self.maskView addGestureRecognizer:tap];
}

- (void)maskViewDidClick {
    [self hideWithAnimation:YES];
}

+ (instancetype)shareInstance {
    static LCAlertPickerView *_instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    });
    return _instance;
}

- (void)showWithTitle:(NSString *)title dataSource:(NSArray *)dataSource complete:(selectedBlock)selected {
    __weak typeof(self) weakSelf = self;
    self.titleLabel.text = title;
    self.dataSource = dataSource;
    self.selected = selected;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.frame = [UIScreen mainScreen].bounds;
    self.alpha = 0;
    self.tipView.frame = CGRectOffset(self.tipView.frame, 0, self.tipView.LC_height);
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.alpha = 1.0;
        weakSelf.tipView.frame = CGRectOffset(weakSelf.tipView.frame, 0, -weakSelf.tipView.LC_height);
    }];
}



#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataSource.count;
}

#pragma mark - UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.dataSource[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    self.selectedStr = self.dataSource[row];
}

#pragma mark - response
- (IBAction)cancel:(id)sender {
    [self hideWithAnimation:YES];
}

- (IBAction)confirm:(id)sender {
    if (self.selected) {
        self.selected(self.selectedStr);
    }
    [self hideWithAnimation:YES];
}

- (void)hideWithAnimation:(BOOL)animtaion {
    if (!animtaion) {
        [self removeFromSuperview];
        return;
    }
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.2 animations:^{
        weakSelf.alpha = 0;
        weakSelf.tipView.frame = CGRectOffset(weakSelf.tipView.frame, 0, weakSelf.tipView.LC_height);
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - setter
- (void)setPickerViewHeight:(CGFloat)pickerViewHeight {
    _pickerViewHeight = pickerViewHeight;
    self.tipViewHeight.constant = pickerViewHeight;
}

@end
